package com.second.controller.reviews;

import com.second.mapper.FShopMapper;
import com.second.model.pojo.FComment;
import com.second.model.pojo.FShop;
import com.second.model.pojo.FUser;
import com.second.moudle.Result;
import com.second.service.IFCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 本类用来演示:
 *
 * @author: MuTing
 * @date: 2022-04-14 21:43
 */

@Controller
public class ReviewsController {

    @Autowired
    private FShopMapper shopMapper;

    @Autowired
    private IFCommentService commentService;

    @RequestMapping("reviews/{shopId}/{orderId}")
    public ModelAndView reviewView(@PathVariable(name = "shopId") Integer shopId,
                                   @PathVariable(name = "orderId") Integer orderId){
        ModelAndView modelAndView = new ModelAndView();
        FShop shop = shopMapper.getById(shopId);
        modelAndView.addObject("shop",shop);
        modelAndView.addObject("orderId",orderId);
        modelAndView.setViewName("jsp/reviews/reviews");
        return modelAndView;
    }

    @PostMapping("addReview")
    @ResponseBody
    private Result addReview(@RequestBody FComment comment, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        FUser user = (FUser) session.getAttribute("user");
        commentService.addComment(comment,user.getId());
        return new Result("200","添加成功");
    }
}
